// components/audioPlayer/index.js
var myaudio = wx.createInnerAudioContext()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    audioInfo: {
      type:Object,
      observer:function(newValue, oldValue) {
        this.audioStop()
        this.setData({
          info:newValue
        })
      }
    },
    hasClose: {
      type: Boolean,
      value: false
    },
    hasTime:{
      type:Boolean,
      value:true
    },
    autoplay:{
      type:Boolean,
      value:true
    }
  
  },

  /**
   * 组件的初始数据
   */
  data: {
    info:{},
    isPlay: false,
    delay: null,
    totalTime: '00:00',
    currentTime: '00:00',
    prevent: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    togglePlay() {
      const isPlay = !this.data.isPlay
      this.setData({
        isPlay
      })
      if (isPlay) {
        this.audioPlay()
      } else {
        this.audioPause()
      }
    },
    audioPlay() {
      const info = this.data.info
      myaudio.src = info.url
      myaudio.autoplay = true
      console.log(myaudio)
      myaudio.play()
      //开始监听
      myaudio.onPlay(() => {
        console.log('开始播放')
        setTimeout(() => {
          myaudio.currentTime
          myaudio.onTimeUpdate(() => {
            console.log(myaudio.duration)
            const totalTime = this.formatTime(myaudio.duration)
            const currentTime = this.formatTime(myaudio.currentTime)
            const prevent = myaudio.currentTime / myaudio.duration * 100
            this.setData({
              totalTime,
              currentTime,
              prevent
            })
          })
        }, 1000)
      })
      myaudio.onError((res) => {
        console.log(res)
      })
      //结束监听
      myaudio.onEnded(() => {
        console.log('播放至结束')
        this.setData({
          isPlay: false
        })
      })
    },
    formatTime(time) {
      var minute = Math.floor(time / 60) % 60;
      var second = Math.floor(time) % 60
      return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    },
    audioPause() {
      myaudio.pause()
      //停止监听
      myaudio.onPause(() => {
        console.log('暂停播放')
        this.setData({
          isPlay: false
        })
      })
    },
    audioStop() {
      myaudio.stop()
      //停止监听
      myaudio.onStop(() => {
        console.log('停止播放')
        this.setData({
          isPlay: false
        })
      })
    },
    sliderChange(e) {
      console.log(e.detail.value) 
      const second = e.detail.value * myaudio.duration / 100
      console.log(second)
      const currentTime = this.formatTime(second)
      this.setData({
        currentTime
      })
      myaudio.seek(second)
    },
    closeAudio(){
      this.audioStop()
      this.triggerEvent('close')
    }
  },
  lifetimes: {
    ready() {
      if(this.properties.autoplay){
        this.setData({
          isPlay:true
        })
        this.audioPlay()
      }
    },detached: function () {
      // 在组件实例被从页面节点树移除时执行
      console.log('页面退出，播放停止')
      myaudio.stop()
    },
  }
})
